﻿Imports ISNet.WebUI.WebGrid
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections.Generic
Imports System.Linq
Imports ISNet.WebUI.WebTreeView
Imports log4net

Partial Class mtCmmtActivities
    Inherits System.Web.UI.Page

    Private Shared logger As ILog = LogManager.GetLogger("mtCmmtActivities")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        '
        ScreenName.Text = "ระบบจัดทำงบประมาณ  >>  ตั้งค่าระบบ >> Commitment Item - Activity"
        'Session("users") = "bb0011"
        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If
        
        If IsPostBack = False Then
            ' Session("PlanYear") = "2557"
            PlanYearsTxt.Text = Session("PlanYear")
            Session("PlanYearTxt") = PlanYearsTxt.Text
            Session("SearchTxt") = ""
            Session("SearchTxt") = " and PlanYear = " & PlanYearsTxt.Text
        End If

    End Sub

    Protected Sub WebGrid1_InitializeDataSource(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.DataSourceEventArgs) Handles WebGrid1.InitializeDataSource
        If Session("SearchTxt") = "" Then
            Exit Sub
        End If
        Dim adapter As New MasterDataSetTableAdapters.CmmtActivitiesTableAdapter
        Dim dtTable As New MasterDataSet.CmmtActivitiesDataTable

        Dim sql As String = "select * from CmmtActivities "
        sql += " WHERE 1 = 1 "

        sql += Session("SearchTxt")
        sql += " ORDER BY PlanYear "

        adapter.SearchCmmtActivity(dtTable, sql)
        ' Response.Write(sql)

        Session("count_data") = dtTable.Rows.Count

        dtTable.Columns("PlanYear").AllowDBNull = True
        dtTable.Columns("Activity_ActivityCode").AllowDBNull = True
        dtTable.Columns("Activity_PlanYear").AllowDBNull = True
        dtTable.Columns("Cmmt_CmmtCode").AllowDBNull = True
        dtTable.Columns("Cmmt_PlanYear").AllowDBNull = True

        e.DataSource = dtTable
    End Sub

    Protected Sub CommtItemCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)
        Dim adap As New MasterDataSetTableAdapters.CmmtsTableAdapter
        Dim dt As New MasterDataSet.CmmtsDataTable
        dt = adap.GetData(Val(Session("PlanYearTxt")))

        e.DataSource = dt
    End Sub
    Protected Sub ActivityWebCombo_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)
        Dim adap As New MasterDataSetTableAdapters.ActivitiesTableAdapter
        Dim dt As New MasterDataSet.ActivitiesDataTable
        dt = adap.GetDataByPlanYear(Val(Session("PlanYearTxt")))

        e.DataSource = dt
    End Sub

    Protected Sub WebGrid1_InitializeRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        ' don't edit column
        e.Row.Cells(1).ForceNoEdit = True
        e.Row.Cells(4).ForceNoEdit = True
    End Sub

    Protected Sub WebGrid1_AddRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        Dim adapter As New MasterDataSetTableAdapters.CmmtActivitiesTableAdapter
        Dim check = adapter.CheckExistCmmtActivities(Val(e.Row.Cells(1).Value), e.Row.Cells(3).Value, Val(Session("PlanYearTxt")), e.Row.Cells(2).Value, Val(Session("PlanYearTxt")))
        'WebGrid1.ClientAction.Alert(e.Row.Cells(0).Value & "<br />" & e.Row.Cells(1).Value & "<br />" & e.Row.Cells(2).Value & "<br />" & Val(Session("PlanYearTxt")))
        'Exit Sub
        If check <> 0 Then
            'WebGrid1.ClientAction.Refresh()
            WebGrid1.ClearCachedDataSource()
            Throw New ArgumentException("ข้อมูลซ้ำกับที่มีอยู่ กรุณาตรวจสอบ")

        Else
            Dim id As Decimal
            id = adapter.InsertCmmtActivities(e.Row.Cells(1).Value, Date.Now, Session("users"), e.Row.Cells(3).Value, Val(Session("PlanYearTxt")), (e.Row.Cells(2).Value), Val(Session("PlanYearTxt")))
            Dim dr As DataRow = e.Row.DataRow
            dr("id") = id
            'WebGrid1.ClientAction.Refresh()

            logger.Info("User " & Session("users") & " insert commitment " & e.Row.Cells(2).Value & " and activity " & e.Row.Cells(3).Value)
        End If
    End Sub

    Protected Sub WebGrid1_UpdateRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        Dim id As String
        id = e.Row.KeyValue
        Dim adapter As New MasterDataSetTableAdapters.CmmtActivitiesTableAdapter
        Dim check = adapter.CheckExistUpdate(Val(e.Row.Cells(1).Value), Val(e.Row.Cells(3).Value), Session("PlanYearTxt"), e.Row.Cells(2).Value, Val(Session("PlanYearTxt")), id)

        If check > 1 Then
            ' WebGrid1.ClientAction.Refresh()
            WebGrid1.ClearCachedDataSource()
            Throw New ArgumentException("ข้อมูลซ้ำกับที่มีอยู่ กรุณาตรวจสอบ")

        Else
            adapter.UpdateCmmtActivities(e.Row.Cells(1).Value, Date.Now, Session("users"), e.Row.Cells(3).Value, Val(Session("PlanYearTxt")), e.Row.Cells(2).Value, Val(Session("PlanYearTxt")), id)
            logger.Info("User " & Session("users") & " update commitment " & e.Row.Cells(2).Value & " and activity " & e.Row.Cells(3).Value)
            'WebGrid1.ClientAction.Refresh()
        End If
    End Sub

    Protected Sub WebGrid1_InitializePostBack(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.PostbackEventArgs)
        If e.Action = PostBackAction.Custom Then
            Dim type As String = TryCast(Request("type"), String)
            Dim Deletet As String = TryCast(Request("id"), String)
            Dim adapter As New MasterDataSetTableAdapters.CmmtActivitiesTableAdapter
            ' MsgBox(Deletet)
            If type = "1" Then

                adapter.DeleteCmmtActivities(Deletet)
                logger.Info("User " & Session("users") & " delete commitment-activity " & Deletet)

            ElseIf type = "2" Then
                For Each keyValue As String In WebGrid1.RootTable.GetCheckedRows()
                    adapter.DeleteCmmtActivities(keyValue)
                Next keyValue
            End If
            WebGrid1.ClientAction.Refresh()
        End If
    End Sub

    Protected Sub SearhBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SearhBtn.Click

        t_countdata.Visible = True
        If Val(Session("count_data")) > 0 Then
            L_CountData.Text = FormatNumber(Val(Session("count_data")), 0) & " รายการ"
        Else
            L_CountData.Text = "ไม่พบข้อมูลที่ค้นหา"
        End If

        Session("SearchTxt") = ""

        If (PlanYearsTxt.Text <> " " And PlanYearsTxt.Text <> "") Then
            Session("SearchTxt") += " and PlanYear = " & PlanYearsTxt.Text
        End If
        If Val(CommtItemCombo.Value) <> 0 Then
            Session("SearchTxt") += " and Cmmt_CmmtCode = " & CommtItemCombo.Value
        End If
        If Val(ActivityWebCombo.Value) <> 0 Then
            Session("SearchTxt") += " and Activity_ActivityCode = " & ActivityWebCombo.Value
        End If
        Session("PlanYearTxt") = PlanYearsTxt.Text
        CommtItemCombo.ClearCachedDataSource()
        CommtItemCombo.DataBind()
        Commitment2WebCombo.ClearCachedDataSource()
        Commitment2WebCombo.DataBind()
        ActivityWebCombo.ClearCachedDataSource()
        ActivityWebCombo.DataBind()
        Activity2Combo.ClearCachedDataSource()
        Activity2Combo.DataBind()
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
        Commitment2ObjectDataSource.DataBind()
        Activity2ObjectDataSource.DataBind()
    End Sub

    Protected Sub SaveBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SaveBtn.Click
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
    End Sub

End Class
